package com.dnjn.mes.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.mes.biz.domain.MesAssembly;
import com.dnjn.mes.biz.domain.MesExcept;
import com.dnjn.mes.biz.domain.MesWorkpiece;
import com.dnjn.mes.biz.mapper.MesAssemblyMapper;
import com.dnjn.mes.biz.service.IMesAssemblyService;
import com.dnjn.mes.biz.service.IMesExceptService;
import com.dnjn.mes.biz.service.IMesResolvePlanService;
import com.dnjn.mes.biz.service.IMesWorkpieceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 组件|模块表 服务实现类
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@Service
public class MesAssemblyServiceImpl extends ServiceImpl<MesAssemblyMapper, MesAssembly> implements IMesAssemblyService {

    @Autowired
    private IMesWorkpieceService workpieceService;

    @Autowired
    private IMesExceptService exceptService;
    @Autowired
    private IMesResolvePlanService resolvePlanService;

    @Override
    public MesAssembly selectByAssemblyName(Long equipmentId, String assemblyName) {

        return lambdaQuery().eq(MesAssembly::getEquipmentId, equipmentId)
                .eq(MesAssembly::getAssemblyName, assemblyName)
                .one();
    }

    @Override
    public Integer getMaxOrdinal() {
        Integer rodinal = 0;
        Integer maxOrdinal = baseMapper.getMaxOrdinal();
        if (!Objects.isNull(maxOrdinal)) {
            rodinal = maxOrdinal;
        }
        return rodinal;
    }

    @Override
    public List<MesAssembly> searchAssemblyList(Long equipmentId) {
        //遍历每个模块下的零部件，确认是否需要保养
        List<MesAssembly> mesAssemblyList = lambdaQuery().eq(MesAssembly::getEquipmentId, equipmentId).eq(MesAssembly::getDeleted,"0").list();
        BigDecimal warnBigDecimal = BigDecimal.valueOf(10);//警告标记
        BigDecimal remindBigDecimal = BigDecimal.valueOf(30);//提醒标记
        BigDecimal bigDecimal = BigDecimal.valueOf(100);
        for (MesAssembly mesAssembly : mesAssemblyList) {
            int flag = 2;//模块状态标识
            List<MesWorkpiece> workpieceList = workpieceService.lambdaQuery().eq(MesWorkpiece::getAssemblyId,
                    mesAssembly.getAssemblyId()).list();
            for (MesWorkpiece mesWorkpiece : workpieceList) {
                //判断零部件是否需要保养，需要的话是警告还是提醒阶段 返回前端判断展示
                //计算寿命百分比   time_spent/maintain_duration
                BigDecimal timeSpent = mesWorkpiece.getTimeSpent();//耗时
                Integer maintainDuration = mesWorkpiece.getMaintainDuration();//保养周期寿命
                BigDecimal multiply = timeSpent.divide(BigDecimal.valueOf(maintainDuration), RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
                //用100-耗时所占百分比，就是剩余寿命百分比
                multiply = bigDecimal.subtract(multiply);
                 if (multiply.compareTo(warnBigDecimal) > 0 && multiply.compareTo(remindBigDecimal) <= 0){//大于10 小于30 提醒
                    flag = 1; //提醒
                }
                 if (multiply.compareTo(warnBigDecimal) <= 0){// <0 小于 | = 0 等于 | >0 大于
                     flag = 0;//警告 直接跳出循环
                     continue;
                }
            }
            mesAssembly.setStatus(flag);
        }
        return mesAssemblyList;
    }

    public MesAssembly queryByCrumb(String equipmentName, String assemblyName) {
        return baseMapper.queryByCrumb(equipmentName, assemblyName);
    }

    @Override
    public Boolean removeAssembly(Long assemblyId) {
        Long count = workpieceService.lambdaQuery().eq(MesWorkpiece::getAssemblyId, assemblyId).eq(MesWorkpiece::getDeleted, "0").count();
        Long count1 = exceptService.lambdaQuery().eq(MesExcept::getAssemblyId, assemblyId).eq(MesExcept::getDeleted, "0").count();
        if(count == 0 && count1==0){
            //软删除模块
            MesAssembly mesAssembly = new MesAssembly();
            mesAssembly.setAssemblyId(assemblyId);
            mesAssembly.setDeleted("1");
            baseMapper.updateById(mesAssembly);
            return true;
        }else {
            return false;
        }
    }
}
